﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;



namespace EMOe
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<Emoticon> favlist = new List<Emoticon>();
        private List<Emoticon> alllist;
        public int cnt = 0;

        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (cnt == 0) {
                loadCate();
            }
            readEmo();
            readFav();
            cnt++;
        }
 
        public void readEmo() {
            lstEmo.ItemsSource = null;
            alllist = new List<Emoticon>();
            XDocument loadedData = XDocument.Load("emocollection.xml");
            string cs = (App.Current as App).cString;
            if (cs.Length != 0) {
                pivotAll.Header = cs;
                foreach (XElement item in loadedData.Elements("emocollection").Elements("emoticon"))
                {
                    Emoticon newemo = new Emoticon();
                    newemo.emo = item.Element("emo").Value;
                    newemo.category = item.Element("category").Value;
                    if (cs == newemo.category)
                    {
                        alllist.Add(newemo);
                    }
                }
                (App.Current as App).cString = "";

            }
            else if (cs.Length == 0) {
                pivotAll.Header = "All";
                foreach (XElement item in loadedData.Elements("emocollection").Elements("emoticon"))
                {
                    Emoticon newemo = new Emoticon();
                    newemo.emo = item.Element("emo").Value;
                    newemo.category = item.Element("category").Value;
                    foreach (Category cate in (App.Current as App).c)
                    {
                        if (cate.cateName == newemo.category && cate.cateSet == true)
                            alllist.Add(newemo);
                    }
                }
            }
            lstEmo.SelectedIndex = -1;
            lstEmo.ItemsSource = alllist;
        }


        public void loadCate()
        {
            XDocument loadedData = XDocument.Load("catecollection.xml");
            foreach (XElement item in loadedData.Elements("catecollection").Elements("category"))
            {
                Category newcate = new Category();
                newcate.cateName = item.Element("cateName").Value;
                newcate.cateSet = true;
                (App.Current as App).c.Add(newcate);
            }
        }

        public void readFav()
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Fav.xml", FileMode.Open))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Emoticon>));
                        favlist = (List<Emoticon>)serializer.Deserialize(stream);
                        lstFav.ItemsSource = null;
                        lstFav.ItemsSource = favlist;
                    }
                }
            }
            catch
            {
            }
        }

        private void saveFav()
        {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Fav.xml", FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Emoticon>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, favlist);
                    }
                }
            }
        }
        
        private void btnFav_Click(object sender, RoutedEventArgs e)
        {
            Emoticon selectobj = ((Button)sender).DataContext as Emoticon;
            Boolean found = false;
            foreach(Emoticon i in favlist){
                if (i.emo == selectobj.emo) {
                    found = true;
                }
            }
            if (!found) {
                favlist.Add(selectobj);
                saveFav();
                readFav();
                //(sender as Button).Visibility = Visibility.Collapsed;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Emoticon selectobj = ((Button)sender).DataContext as Emoticon;
            if (favlist.Contains(selectobj))
            {
                favlist.Remove(selectobj);
                saveFav();
                readFav();
                //not back :(
            }
        }
        
        private void lstEmo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                Emoticon selectobj = (Emoticon)lstEmo.SelectedItem;
                Clipboard.SetText(selectobj.emo);
                txtEmo.Text = selectobj.emo;
                playCopy.Begin();
            }
            catch
            {
            
            }
            
        }

        private void lstFav_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                Emoticon selectobj = (Emoticon)lstFav.SelectedItem;
                Clipboard.SetText(selectobj.emo);
                txtEmo.Text = selectobj.emo;
                playCopy.Begin();
            }
            catch
            {

            }
        }

        private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Categories.xaml", UriKind.RelativeOrAbsolute));
        }

        
    }
} 